﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JChatCore.City;
using JChatData.CityMap;
using JChatData.Helper;

namespace JChatSerVices.CityService
{
    public partial class CityService:ICityService
    {
        DBConectString _db;
        GetConnect.GetFileConnect _get;
        public CityService()
        {
            _get = new GetConnect.GetFileConnect();
            string file = _get.GetFile("conn");
            _db = new DBConectString(file);
        }
        #region Get Data
        public List<City> GetAllCity()
        {
            var _city = (from c in _db._context.JChat_City
                       select c).ToList();
            return _city;
        }
        public List<City> GetCityByID(int _id)
        {
            var _city = (from c in _db._context.JChat_City
                           where c.CityID==_id
                           select c).ToList();
            return _city;
        }
        #endregion
        #region Add / Update / Delete
        public bool AddCity(City _city)
        {
            try
            {
                _db._context.JChat_City.AddObject(_city);
                _db._context.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool UpdateCity(int _id, City _citytable)
        {
            try
            {
                var _city = (from c in _db._context.JChat_City
                             where c.CityID == _id
                             select c).SingleOrDefault();
                _city.NameCity = _city.NameCity;
                _db._context.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteCity(int _id)
        {
            try
            {
                var _city = (from c in _db._context.JChat_City
                             where c.CityID == _id
                             select c).SingleOrDefault();
                _db._context.DeleteObject(_city);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion 
    }
}
